package com.xw.util;

import com.sun.mail.util.MailSSLSocketFactory;
import com.sun.org.apache.xpath.internal.SourceTree;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Scanner;

/**
 * @author wangxiaoxiao
 */
public class MailUtil {

    public static void sendMailByGmail(String subject, String text, String... toEmail) throws Exception{
        String gmail = PropertiesUtil.getProperty("GMAIL");
        String pwd = PropertiesUtil.getProperty("PASS");
        final String host = "smtp.gmail.com";
        final String from = gmail;
        final String pass = pwd;
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        // Get a Properties object
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", host);
        props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.setProperty("mail.smtp.socketFactory.fallback", "false");
        props.setProperty("mail.smtp.localhost", "127.0.0.1");
        props.setProperty("mail.smtp.port", "465");
        props.setProperty("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));

        InternetAddress[] toAddress = new InternetAddress[toEmail.length];

        // 获取地址的array
        for( int i=0; i < toEmail.length; i++ ) { // 从while循环更改而成
            toAddress[i] = new InternetAddress(toEmail[i]);
        }
        System.out.println(Message.RecipientType.TO);

        for( int i=0; i < toAddress.length; i++) { // 从while循环更改而成
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }
        message.setSubject(subject);
        message.setText(text);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    /**
     *  使用QQ邮箱发送短信
     * @param toEmail 发送人邮箱
     * @param subject 主题
     * @param text    内容
     * @throws Exception
     */
    public static void sendMailByQQ(String toEmail,String subject,String text) throws Exception{
        Properties props = new Properties();
        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.qq.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);
        Session session = Session.getInstance(props);
        Message msg = new MimeMessage(session);
        /*设置主题*/
        msg.setSubject(subject);
        /*设置邮件内容*/
        msg.setText(text);
        msg.setFrom(new InternetAddress("125613552@qq.com"));

        Transport transport = session.getTransport();
        /*服务器地址 邮箱账号 邮箱秘钥*/
        transport.connect("smtp.qq.com", "125613552@qq.com", "ylhuazrgcoficaia");

        transport.sendMessage(msg, new Address[] { new InternetAddress(toEmail) });
        transport.close();
    }
    /**
     *  使用163邮箱发送短信
     * @param toEmail 发送人邮箱
     * @param subject 主题
     * @param text    内容
     * @throws Exception
     */
    public static void sendMailBy163(String toEmail,String subject,String text) throws Exception{
        Properties prop=new Properties();
        prop.put("mail.host","smtp.163.com" );
        prop.put("mail.transport.protocol", "smtp");
        prop.put("mail.smtp.auth", true);
        //使用java发送邮件5步骤
        //1.创建sesssion
        Session session=Session.getInstance(prop);
        //开启session的调试模式，可以查看当前邮件发送状态
        session.setDebug(true);
        //2.通过session获取Transport对象（发送邮件的核心API）
        Transport ts=session.getTransport();
        //3.通过邮件用户名密码链接 账号和授权码
        ts.connect("Sea_wxx@163.com", "seawxx1314");
        //4.创建邮件
        //创建邮件对象
        MimeMessage mm=new MimeMessage(session);
        //设置发件人
        mm.setFrom(new InternetAddress("Sea_wxx@163.com"));
        //设置收件人
        mm.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
        //设置抄送人
//        mm.setRecipient(Message.RecipientType.CC, new InternetAddress("用户名@163.com"));

        mm.setSubject(subject);
        mm.setContent("您本次验证码："+text, "text/html;charset=utf-8");
        //5.发送电子邮件
        ts.sendMessage(mm, mm.getAllRecipients());
    }

    public static void main(String[] args) throws Exception {

//        MailUtil.sendMailByQQ("3185157@qq.com","找回密码!","请勿回复本邮件.点击下面的链接,重设密码 http://www.baidu.com");
//        MailUtil.sendMailBy163("3185157@qq.com","验证码","123456");
        MailUtil.sendMailByGmail("验证码","123456",new String[]{"125613552@qq.com","3185157@qq.com"});
    }
}
